//
//  QZSeeHistoryVC.m
//  daojiVideo
//
//  Created by tikeios_002 on 2024/5/15.
//

#import "QZSeeHistoryVC.h"
#import "XRWaterfallLayout.h"
#import "QZFindsChildCell.h"
#import "SYCollectionViewRoundFlowLayout.h"
#import "CSJADLoading.h"
//#import "DJMyVideoPlayVC.h"

@interface QZSeeHistoryVC ()<UICollectionViewDelegate,UICollectionViewDataSource,XRWaterfallLayoutDelegate>
@property (strong, nonatomic)  UICollectionView *historyCollection;
@property (strong,nonatomic)NSMutableArray <QZVideoModel *>* listArr;
@property (strong,nonatomic) PublicView * nullView;
@property (assign,nonatomic) NSInteger page;
@property (assign, nonatomic) CGFloat itemWidth;

@end

@implementation QZSeeHistoryVC

-(void)showTextViewNavViewTo1{
    NSMutableArray * showTextViewNavViewNameArrTo1 = [NSMutableArray new];
    UIView * showTextViewNavViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewNavViewViewTo1.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewNavViewViewTo1];
    UILabel * showTextViewNavViewLabelTo1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSInteger showTextViewNavViewNumberTo1 = arc4random_uniform(20);
    NSLog(@"：%ld", (long)showTextViewNavViewNumberTo1);
    showTextViewNavViewLabelTo1.text = [NSString stringWithFormat:@"%ld",(long)showTextViewNavViewNumberTo1];
    [showTextViewNavViewLabelTo1 addSubview:showTextViewNavViewLabelTo1];
    
    UIImageView * showTextViewNavViewLabelTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [showTextViewNavViewViewTo1 addSubview:showTextViewNavViewLabelTo2];
    
}
- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"历史浏览";
    
    self.view.backgroundColor = viewback_black;
    self.itemWidth = (KScreen_W-3)/2;
    
    UIButton * rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
    [rightButton setTitle:@"清除" forState: UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightButton addTarget:self action:@selector(historyRemove) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    KWeakSelf
    
    self.historyCollection.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page = weakSelf.page+1;
        [weakSelf getHistoryVideoList];
    }];
    self.historyCollection.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        //下拉刷新要做的操作.
        [weakSelf.historyCollection.mj_footer resetNoMoreData];
        [weakSelf.listArr removeAllObjects];
        [weakSelf.historyCollection reloadData];
        weakSelf.page = 0;
        [weakSelf getHistoryVideoList];
    }];
    [SVProgressHUDTool showWithStatus:nil];
    [self getHistoryVideoList];
}
#pragma mark 懒加载
- (NSMutableArray *)showTextViewToKey1:(NSString *)keyString{
    NSMutableArray *showTextViewArrTo1 = [NSMutableArray new];
    for (int i = 0; [showTextViewArrTo1 count] < 5; i++) {
        int  RandomNumber = arc4random() % 30;
        if(![showTextViewArrTo1 containsObject:[NSNumber numberWithInt:RandomNumber]]){
            [showTextViewArrTo1 addObject:[NSNumber numberWithInt:RandomNumber]];
        }
    }
    return showTextViewArrTo1;
}
- (NSInteger)showTextViewToTime1:(NSString *)time{
    NSDateFormatter *showTextViewFaTo1 = [[NSDateFormatter alloc] init];
    [showTextViewFaTo1 setDateFormat:@"yyyy-MM-dd"];
    return [[showTextViewFaTo1 dateFromString:time] timeIntervalSince1970];
}
- (NSMutableArray *)listArr{
    if(_listArr == nil){
        _listArr = [NSMutableArray new];
    }
    return _listArr;
}
- (UICollectionView *)historyCollection{
    if(_historyCollection == nil){
        //创建瀑布流布局
        XRWaterfallLayout *waterfall = [XRWaterfallLayout waterFallLayoutWithColumnCount:2];
        //或者一次性设置
        [waterfall setColumnSpacing:3 rowSpacing:3 sectionInset:UIEdgeInsetsMake(5, 0, 5+KSafeAreaBottomHeight, 0)];
        //设置代理，实现代理方法
        waterfall.delegate = self;
        _historyCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, KScreen_W, KScreen_H-KNav_H) collectionViewLayout:waterfall];
        _historyCollection.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [_historyCollection registerClass:[QZFindsChildCell class] forCellWithReuseIdentifier:@"QZFindsChildCell"];
        _historyCollection.backgroundColor = viewback_black;
        _historyCollection.alwaysBounceVertical = YES;
        _historyCollection.delegate = self;
        _historyCollection.dataSource = self;
           UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:_historyCollection];
    }
    return _historyCollection;
}
- (PublicView *)nullView{
    if(_nullView == nil){
        _nullView = [[PublicView alloc]initWithFrame:CGRectMake(0, (KScreen_H-KNav_H-200)/2, KScreen_W, 200)];
        _nullView.publicLabel.text = @"暂无历史记录";
        _nullView.hidden = YES;
        _nullView.userInteractionEnabled = NO;
           UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:_nullView];
    }
    return _nullView;
}
#pragma mark 请求区域
- (void)getHistoryVideoList{
    KWeakSelf
    KMutableDic(sendDic)
    sendDic[@"page"] = @(self.page);
    [YBNetBase historyVideoListRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            [SVProgressHUDTool dismiss];
            CGFloat maxLineHeight = Font(14).lineHeight * 3;
            for (NSDictionary * dic in data[@"list"]) {
                if(![dic[@"video_url"] isKindOfClass:[NSDictionary class]] || dic[@"video_url"] == nil || [dic[@"video_url"] allKeys].count == 0){
                    continue;
                }
                QZVideoModel * model = [[QZVideoModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                model.introHeight = [Tools getTextHeightWithText:model.introduce width:weakSelf.itemWidth-20 maxHeight: maxLineHeight font:Font(14)] + 2;
//                CGFloat imageH = [model.height floatValue]/[model.width floatValue]*weakSelf.itemWidth;
                CGFloat imageH;
                if([model.height floatValue] > [model.width floatValue]){
                    imageH = self.itemWidth/3*4;
                }else{
                    imageH = self.itemWidth/4*3;
                }
                model.itemSize = CGSizeMake(weakSelf.itemWidth, model.introHeight+imageH+56);
                [weakSelf.listArr addObject:model];
            }
            [weakSelf.historyCollection reloadData];
            
            if([weakSelf.historyCollection.mj_header isRefreshing]){
                [weakSelf.historyCollection.mj_header endRefreshing];
            }else if ([weakSelf.historyCollection.mj_footer isRefreshing]){
                if([data[@"list"] count] == 0){
                    [weakSelf.historyCollection.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [weakSelf.historyCollection.mj_footer endRefreshing];
                }
            }else{
                if(weakSelf.listArr.count == 0){
                    weakSelf.nullView.hidden = NO;
                    weakSelf.historyCollection.mj_footer.hidden = YES;
                }else{
                    weakSelf.nullView.hidden = YES;
                    weakSelf.historyCollection.mj_footer.hidden = NO;
                }
            }
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}

-(void)showTextViewAddShowViewTo2{
    UIView * showTextViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 100, 360)];
    showTextViewViewTo1.backgroundColor = [UIColor whiteColor];
    showTextViewViewTo1.layer.cornerRadius = 8;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewViewTo1];
    
    UIImageView * showTextViewimgTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(K_AUTOLAYOUT_W(157.5), K_AUTOLAYOUT_W(260), K_AUTOLAYOUT_W(60), K_AUTOLAYOUT_W(60))];
    showTextViewimgTo2.layer.cornerRadius = K_AUTOLAYOUT_W(30);
    showTextViewimgTo2.clipsToBounds = YES;
    showTextViewimgTo2.contentMode = UIViewContentModeScaleAspectFill;
    showTextViewimgTo2.hidden = YES;
    [showTextViewViewTo1 addSubview:showTextViewimgTo2];
    
    UILabel *  showTextViewimgTo3 = [[UILabel alloc]initWithFrame:CGRectMake(K_AUTOLAYOUT_W(15), K_AUTOLAYOUT_W(35), K_AUTOLAYOUT_W(345), K_AUTOLAYOUT_W(25))];
    showTextViewimgTo3.font = FontBold(18);
    showTextViewimgTo3.textColor = ThreeColor;
    showTextViewimgTo3.textAlignment = NSTextAlignmentCenter;
    showTextViewimgTo3.hidden = YES;
    [showTextViewViewTo1 addSubview:showTextViewimgTo3];
}
- (NSString *)showTextViewGetNowTimerTo3{
    NSDateFormatter *showTextViewGetNowFa = [[NSDateFormatter alloc] init];
    [showTextViewGetNowFa setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [showTextViewGetNowFa stringFromDate:[NSDate date]];
}

- (void)tableEndFiresh{
    if([self.historyCollection.mj_header isRefreshing]){
        [self.historyCollection.mj_header endRefreshing];
    }
    if([self.historyCollection.mj_footer isRefreshing]){
        [self.historyCollection.mj_footer endRefreshing];
    }
}
#pragma mark 事件区域
- (void)historyRemove{
    KWeakSelf
    [[BSystemAlert shared] showAlertWithTitle:@"温馨提示" message:@"是否确定清除所有历史观看记录？" userData:nil cancelTitle:@"取消" actions:@[@"清除"] actionBlock:^(id  _Nonnull userData, NSString * _Nonnull actionTitle) {
        if([actionTitle isEqualToString:@"清除"]){
            KMutableDic(sendDic)
            [YBNetBase historyVideoRemoveRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
                if(code == 200){
                    [weakSelf.listArr removeAllObjects];
                    [weakSelf.historyCollection reloadData];
                    weakSelf.nullView.hidden = NO;
                }else{
                    [YBMsgPop showPop:msg];
                }
            }];
        }
    }];
}
#pragma mark collectionDelege
//根据item的宽度与indexPath计算每一个item的高度
- (CGFloat)waterfallLayout:(XRWaterfallLayout *)waterfallLayout itemHeightForWidth:(CGFloat)itemWidth atIndexPath:(NSIndexPath *)indexPath {

    return self.listArr[indexPath.row].itemSize.height;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.listArr.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    QZFindsChildCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QZFindsChildCell" forIndexPath:indexPath];
    [cell findsShoeCell:self.listArr[indexPath.row]];
    return cell;
}

- (void)showTextViewSubViewToViewTo1:(UIView * )subView{

    UIView * showTextViewSubViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewSubViewTo1.clipsToBounds = YES;
    [subView addSubview:showTextViewSubViewTo1];
    UILabel * showTextViewSubViewLabelTo2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    showTextViewSubViewLabelTo2.text = @"56464645";
    [showTextViewSubViewTo1 addSubview:showTextViewSubViewLabelTo2];
    
    UIImageView * showTextViewSubViewTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [subView addSubview:showTextViewSubViewTo2];

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    DJMyVideoPlayVC * play = [[DJMyVideoPlayVC alloc]init];
//    play.pushArr = self.listArr;
//    play.currenIndex = indexPath.row;
//    [self.navigationController pushViewController:play animated:YES];
}
@end
